#!/bin/bash

# generating the input files
function copy_file() {
	
	local numfiles=$1
	local basefile=$2
	local dst=$3
	local list_file=$4
		
	for i_file in `seq 1 $numfiles`
	do
		tmp_name=$dst"/"`basename $basefile`"_"$i_file
		echo "creating input file "$tmp_name
		if [ ! -f $tmp_name ]
		then
			echo "the file: "${tmp_name}" already exists"
			cp $basefile $tmp_name
		fi
		echo $tmp_name >> $list_file
	done
}

function killjava() {
	
	local nodesfile=$1
	
	{ while read node;do
		echo "killing processes on node: "$node
		ssh $node "killall java"
	done } < $nodesfile
}

function startworkers() {	
	
	local workernodes=$1
	local falkonpath=$2
	local servicenode=$3
	
	{ while read node;do
		sleep 5
		echo "starting a workernode on: "$node " using servicenode: "$servicenode
		ssh $node "cd ${falkonpath} ; source falkon.env ; bash falkon-worker-caching-stdout.sh ${servicenode} 50001" >/dev/null &
	done } < $workernodes
}

function startservice() {
	
	local servicenode=$1
	local falkonpath=$2
	
	echo "starting falkon service on node: "$servicenode
	ssh $servicenode "cd ${falkonpath} ; source falkon.env ; bash falkon-service-stdout.sh 50001 config/Falkon-caching.config" &> /dev/null &
}

function runjob() {
	
	local jobfile=$1
	local servicenode=$2
	local clientnode=$3
	local falkonpath=$4
	local n_execnodes=$5
	
	ssh $clientnode "cd ${falkonpath} ; source falkon.env ; bash falkon-client-caching-debug.sh ${servicenode} 50001 ${jobfile} $n_ex_nodes $n_ex_nodes $n_ex_nodes $n_ex_nodes $n_ex_nodes"
}
